package com.example.expandablelistview.customwidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

public class MeasView extends View {
	StringBuilder mResult = new StringBuilder();

	public MeasView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MeasView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MeasView(Context context) {
		super(context);
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.RED);
	}

	private String Spec2String(int spec) {
		StringBuilder str = new StringBuilder();
		switch (MeasureSpec.getMode(spec)) {
		case MeasureSpec.AT_MOST:
			str.append("AT_MOST");
			break;
		case MeasureSpec.EXACTLY:
			str.append("EXACTLY");
			break;
		case MeasureSpec.UNSPECIFIED:
			str.append("UNSPECIFIED");
			break;
		}
		str.append(" " + MeasureSpec.getSize(spec));
		return str.toString();
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int wMode;
		int hMode;
		int wSpec;
		int hSpec;
		//채워질 내용물의 임의 폭과 높이
		int width = 150;
		int height = 80;

		wMode = MeasureSpec.getMode(widthMeasureSpec);
		wSpec = MeasureSpec.getSize(widthMeasureSpec);
		hMode = MeasureSpec.getMode(heightMeasureSpec);
		hSpec = MeasureSpec.getSize(heightMeasureSpec);

		switch (wMode) {
		case MeasureSpec.AT_MOST:
			width = Math.min(wSpec, width);
			break;
		case MeasureSpec.EXACTLY:
			width = wSpec;
			break;
		case MeasureSpec.UNSPECIFIED:
			break;
		}

		switch (hMode) {
		case MeasureSpec.AT_MOST:
			height = Math.min(hSpec, height);
			break;
		case MeasureSpec.EXACTLY:
			height = hSpec;
			break;
		case MeasureSpec.UNSPECIFIED:
			break;
		}
		
		setMeasuredDimension(width, height);
		mResult.append(Spec2String(widthMeasureSpec) + ", "
				+ Spec2String(heightMeasureSpec) + " -> (" + width + ", "
				+ height + ")\n");
	}
}
